import { CordModel, ThingSizeModel } from "./base/model";


export class Map {
    private contentWidthUnits = -1;
    private contentHeightUnits = 15;
    public blockArr: CordModel[] = []; // 不可破坏的砖头
    public softBlockArr: CordModel[] = []; // 可破坏的砖头
    private softGraphics = new PIXI.Graphics();
    public wallSize = 20;

    constructor(private app: PIXI.Application) {
        this.contentWidthUnits = Math.floor(app.view.width / this.wallSize);

        this.blockArr = this.initBlockArr();

        // todo: asd
        this.blockArr.push({x:1,y:0});
        this.blockArr.push({ x: 2, y: 0 });
        this.blockArr.push({ x: 3, y: 0 });
        this.blockArr.push({ x: 4, y: 0 });
        this.createWall(this.blockArr);


        this.softBlockArr = this.initSoftBlockArr();
        this.softGraphics = this.createWall(this.softBlockArr, true);
    }

    private initSoftBlockArr(): CordModel[] {
        let arr: CordModel[] = [];

        for (let i = 0; i < this.contentWidthUnits; i++) {
            for (let j = 0; j < this.contentHeightUnits; j++) {
                if (this.blockArr.findIndex(item => item.x === i && item.y === j) < 0) {
                    arr.push({ x: i, y: j });
                }
            }
        }

        return arr;
    }

    private initBlockArr(): CordModel[] {
        let arr: CordModel[] = [];

        // 底边
        for (let i = 0; i < this.contentWidthUnits; i++) {
            if (i > 5 && i < 15) {
                continue;
            }
            arr.push({ x: i, y: 25 });
        }

        // 两侧
        for (let i = 0; i < this.contentHeightUnits; i++) {
            arr.push({ x: 0, y: i });
            arr.push({ x: this.contentWidthUnits - 1, y: i });
        }
        return arr;
    }

    /**
     * 绘制墙面，不可被破坏
     */
    private createWall(arr: CordModel[], isSoft = false): PIXI.Graphics {
        let lineColor = 0xFEEB77;
        let fillColor = 0x650A5A;
        const graphics = new PIXI.Graphics();

        arr.forEach(item => {
            graphics.lineStyle(2, lineColor, 1);
            if (!isSoft) {
                graphics.beginFill(fillColor);
            } else {
                graphics.beginFill(0xffffff);
            }
            graphics.drawRect(item.x * this.wallSize, item.y * this.wallSize, this.wallSize, this.wallSize);
            graphics.endFill();
        });

        this.app.stage.addChild(graphics);
        return graphics
    }

    public refreshSoftWall(){
        this.app.stage.removeChild(this.softGraphics);
        this.softGraphics = this.createWall(this.softBlockArr, true);
    }
}
